﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Services;
using NS.BLL.Systems;
using NS.Model.Systems;
using NS.Utilities;

public partial class Systems_UserManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridBind();
            SysRolesBind();
        }
    }
    public void SysRolesBind()
    {
        SysRole.DataSource = RoleService.GetRoleByWeights(100);
        SysRole.DataBind();

        ddlRole.DataSource = RoleService.GetRoleByWeights(100);
        ddlRole.DataTextField = "RoleName";
        ddlRole.DataValueField = "RoleName";
        ddlRole.DataBind();
        ListItem li = new ListItem("请选择", "");
        ddlRole.Items.Insert(0, li);
    }

    public void GridBind()
    {
        UserSearch UserSelect = new UserSearch();
        
        UserSelect.Mobile_equal = Mobile.Text.Trim();
        UserSelect.TrueName_like = TrueName.Text.Trim();
        if (ddlRole.SelectedValue != "")
        {
            UserSelect.RoleName = ddlRole.SelectedItem.Value;
        }
        FGridView1.SQLString = UserDetailService.GetListStr() + UserSelect.FinalSQL;
        FGridView1.PKey = "UserID";
        FGridView1.OrderName = "OrderNum";
        FGridView1.BindData();
        FGridView1.AddControl();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserDetail UserModel = new UserDetail();
        UserModel.Mobile = txtMobile.Text.Trim();
        UserModel.PassWord = EncryptHelper.Encrypt(UserModel.Mobile);
        UserModel.TrueName = txtTrueName.Text.Trim();
        UserModel.Sex = ddlSex.SelectedValue;
        UserModel.CardNumber = txtCardNumber.Text.Trim();
        UserModel.Birthday = txtBirthday.Text;
        UserModel.WeChat = txtWeChat.Text.Trim();
        UserModel.OrderNum = txtPaiXu.Text;
        UserModel.IsLock = 0;
        int UserID = UserDetailService.Insert(UserModel);
        if (UserID > 0)
        {
            foreach (ListItem UserRole in SysRole.Items)
            {
                if (UserRole.Selected)
                {
                    UsersInRolesService.AddUsersInRoles(UserID, UserRole.Value);
                }
            }
            GridBind();
            MessageBox.ShowSaveSuccess(this);
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GridBind();
    }
    protected void FGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex >= 0)
        {
          
        }
    }

    protected void btnRePWD_Click(object sender, EventArgs e)
    {
        string strErr = "重置失败！";
        string UserID = ((Button)sender).CommandArgument.ToString();
        UserDetail Model = UserDetailService.Select(UserID);
        if (Model != null)
        {
            Model.PassWord = EncryptHelper.Encrypt(Model.Mobile);
            if (UserDetailService.Update(Model) > 0)
            {
                strErr = "密码已重置为手机号！";
            }
        }
        MessageBox.ShowInfo(this, strErr);
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string userID = ((Button)sender).CommandArgument.ToString();
        if (UserDetailService.Delete(userID) > 0)
        {
            UsersInRolesService.DeleteUsersInRolesByUserID(long.Parse(userID));
            GridBind();
        }
        else
        {
            MessageBox.ShowError(this, "删除失败，请稍后重试！");
        }
    }
}